﻿using HARTCalibrationTool.SerialPortManager;
using MathNet.Numerics.Distributions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HARTCalibrationTool.Observers
{
    public class RetestAnalyzeExcelUpdater : IObserver
    {
        private ExcelHelper _excelHelper;

        public RetestAnalyzeExcelUpdater(ref ExcelHelper excelHelper)
        {
            _excelHelper = excelHelper;
        }

        public void Update(List<string> listStr)
        {
            if (listStr.Count == 0)
            {
                return;
            }

            string[] info = null;
            int chl = 0;

            info = listStr[0].Split('#');

            if (int.TryParse(info[0], out chl))
            {

            }
            else
            {
                return;
            }

            if (info[1] == "PV")
            {
                if (info[2] == "Up")
                {
                    _excelHelper.upRetestPVData[chl].Add(info[3]);
                }
                else if (info[2] == "Down")
                {
                    _excelHelper.downRetestPVData[chl].Add(info[3]);
                }
            }
            else if (info[1] == "Current")
            {
                if (info[2] == "Up")
                {
                    _excelHelper.upRetestCurrentData[chl].Add(info[3]);
                }
                else if (info[2] == "Down")
                {
                    _excelHelper.downRetestCurrentData[chl].Add(info[3]);
                }
            }
            else
            {

            }
        }
    }

    public class RetestDataExcelUpdater : IObserver
    {
        private ExcelHelper _excelHelper;

        public RetestDataExcelUpdater(ref ExcelHelper excelHelper)
        {
            _excelHelper = excelHelper;
        }

        public void Update(List<string> listStr)
        {
            if (listStr.Count == 0)
            {
                return;
            }

            string[] info = null;
            int chl = 0xff;
            string dataType = null;
            double data = 0.0;

            info = listStr[0].Split('#');

            if (int.TryParse(info[0], out chl))
            {

            }
            else
            {
                return;
            }

            dataType = info[1];

            if (double.TryParse(info[2], out data))
            {

            }
            else
            {
                return;
            }

            try
            {
                if (dataType == "PV")
                {
                    _excelHelper.retestData[chl].Add(new List<string> {
                    (_excelHelper._currentDataCnt[chl] + 1).ToString(),     // No.
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),    // 时间
                    "",    // PV
                    "",    // mA
                    ""});  // 温度

                    _excelHelper.retestData[chl][_excelHelper._currentDataCnt[chl]][2] = info[2];
                }
                else if (dataType == "Current")
                {
                    _excelHelper.retestData[chl][_excelHelper._currentDataCnt[chl]][3] = info[2];

                    _excelHelper._currentDataCnt[chl]++;
                }
            }
            catch (Exception ex)
            {
                SerialPortsBase.PrintMessage(ex.Message);
                SerialPortsBase.PrintMessage($"chl: {chl}");
                SerialPortsBase.PrintMessage($"_excelHelper._currentDataCnt[chl]: {_excelHelper._currentDataCnt[chl]}");
            }
        }
    }
}
